package com.sykj.bsc.zjj.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.bsc.rwl.po.SysUser;
import com.sykj.bsc.util.JsonUtils;
import com.sykj.bsc.zjj.bean.Bis_shop_post;
import com.sykj.bsc.zjj.bean.Bis_shop_user;
import com.sykj.bsc.zjj.service.Bis_shop_postService;

@Controller
@RequestMapping("zjj")
public class Bis_shop_postController {
	@Autowired
	private Bis_shop_postService bsps;
	
	/**
	 * 查询
	 * 店铺职位信息表
	 * @param map
	 * @param bsu
	 * @return
	 */
	@RequestMapping("selectBis_shop_post")
	public String selectBis_shop_post(Map<String, Object> map,Model model,Bis_shop_post bsp,@RequestParam(value = "page", defaultValue = "1") Integer page) {
		PageHelper.startPage(page,5);
		
		//定值
		if(bsp!=null) getBis_shop_post1(model, bsp);
		
		/**
		 * 生成随机用户编号
		 *//*
		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
		String str=sdf.format(new Date());
		Random random=new Random();
		int num= random.nextInt(9999)+1000;
		String post_id=str+num;
		map.put("post_id", post_id);*/
		
		List<Map> list = this.bsps.selectBis_shop_post(bsp);
		//map.put("list", list);
		PageInfo<Map> info = new PageInfo<>(list);
		map.put("info", info);
		return "backstage/zjj/positionselect";		
	}
	
	/**
	 * 条件查询定值方法
	 * @param model
	 * @param bsp
	 */
	private void getBis_shop_post1(Model model,Bis_shop_post bsp) {
		if (bsp.getPost_code()!=null && !bsp.getPost_code().equals("")) {
			model.addAttribute("post_code", bsp.getPost_code());
		}
		if (bsp.getPost_name()!=null && !bsp.getPost_name().equals("")) {
			model.addAttribute("post_name", bsp.getPost_name());
		}
	}
	
	/**
	 * 添加
	 * 店铺职位信息表
	 * 修改
	 * @param bis_shop_user
	 * @return
	 */
	@RequestMapping("insertBis_shop_post")
	public String insertBis_shop_post(Bis_shop_post bis_shop_post,HttpSession session) {
		String userId=this.getUser(session);
		if (bis_shop_post.getPost_id()!=null&&!bis_shop_post.getPost_id().equals("")) {//修改
			//session
			bis_shop_post.setModify_user_id(userId);
			//修改的中间表session在service实现类中
			this.bsps.updateBis_shop_post(bis_shop_post,userId);
		} else {//添加
			//获取数据库当前表 最大ID
			String id = this.bsps.getId();
			//当前最大ID加1
			int nowid = Integer.valueOf(id)+1;
			bis_shop_post.setPost_code(nowid+"");
			
			//session
			bis_shop_post.setCreate_user_id(userId);
			//添加的中间表session在service实现类中
			this.bsps.insertBis_shop_post(bis_shop_post,userId);
		}
		return "redirect:/zjj/selectBis_shop_post";
	}
	
	private String getUser(HttpSession session) {
		SysUser sysUser=(SysUser)session.getAttribute("user");
		String userId=sysUser.getUser_id()+"";
		return userId;
	}
	
	/**
	 * 根据ID查询
	 * Bis_shop_post
	 * 店铺职位信息表
	 * 打开修改模态框
	 * @param post_id
	 * @param resp
	 * @throws IOException
	 */
	/*@RequestMapping("findByIdBis_shop_post")
	public void findByIdBis_shop_post(String post_id,HttpServletResponse resp) throws IOException {
		String data=JsonUtils.objectToJson(this.bsps.findByIdBis_shop_post(post_id));
		PrintWriter out=resp.getWriter();
		out.println(data);
		out.flush();
		out.close();
	}*/
	@RequestMapping("findByIdBis_shop_post")
	public void findByIdBis_shop_post(String post_id,HttpServletResponse response){
		this.getWirte(response, this.bsps.findByIdBis_shop_post(post_id));
	}
	public void getWirte(HttpServletResponse response,Object data) {
		try {
			String json=JsonUtils.objectToJson(data);
			PrintWriter	out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//以下是不需要自定义方法的写法 但是需要相应的jar包依赖
	/*@RequestMapping("findByIdBis_shop_post")
	@ResponseBody
	public Bis_shop_post findByIdBis_shop_post(String post_id){
		System.out.println(this.bsps.findByIdBis_shop_post(post_id));
		return this.bsps.findByIdBis_shop_post(post_id);
	}*/
	
	/**
	 * 修改
	 * Bis_shop_post
	 * 店铺职位信息表
	 * @param bis_shop_post
	 * @return
	 */
	/*@RequestMapping("updateBis_shop_post")
	public String updateBis_shop_post(Bis_shop_post bis_shop_post) {
		this.bsps.updateBis_shop_post(bis_shop_post);
		return "redirect:/zjj/selectBis_shop_post";
	}*/
	
	/**
	 * 删除
	 * 根据ID删除
	 * Bis_shop_post
	 * 店铺职位信息表
	 * @param post_id
	 * @return
	 */
	//前台点击标签向后台发送请求 没有任何提示 直接删除
	/*@RequestMapping("deleteBis_shop_post/{post_id}")
	public String deleteBis_shop_post(@PathVariable("post_id") String post_id) {
		this.bsps.deleteBis_shop_post(post_id);
		return "redirect:/zjj/selectBis_shop_post";		
	}*/
	@RequestMapping("deleteBis_shop_post")
	public String deleteBis_shop_post(String post_id) {
		this.bsps.deleteBis_shop_post(post_id);
		return "redirect:/zjj/selectBis_shop_post";
	}
	
	/**
	 * 查询店铺职位信息表
	 * 判断职位编码是否重复
	 * @param mobile
	 * @param request
	 * @param response
	 */
	@RequestMapping("findByIdBis_shop_postpost_code")
	public void findByIdBis_shop_postpost_code(Bis_shop_user post_code,HttpServletRequest request,HttpServletResponse response) {
		String post_code1=request.getParameter("post_code");
		Integer post_code11 = this.bsps.findByIdBis_shop_postpost_code(post_code1);
		boolean b1;
		if (post_code11==0) {
			b1 = true;
		} else {
			b1 = false;
		}
		Map map=new HashMap<>();
		map.put("valid", b1);
		resjson(response, map);
	}
	private void resjson(HttpServletResponse response,Object obj){
    	ObjectMapper  om=new ObjectMapper();
    	try {
			String json=om.writeValueAsString(obj);
			PrintWriter out=response.getWriter();
			out.write(json);
			out.flush();
			out.close();
			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
	
}
